//
//  HMSportSpeaker.m
//  BlackHorseLine
//
//  Created by made on 17/3/13.
//  Copyright © 2017年 made. All rights reserved.
//

#import "HMSportSpeaker.h"
#import <AVFoundation/AVFoundation.h>


@interface HMSportSpeaker ()
//语音合成器
@property(nonatomic,strong)AVSpeechSynthesizer *synthesizer;
//运动类型
@property(nonatomic,assign)HMSportType sportType;

//运动类型字符串
@property(nonatomic,strong)NSString *sportTypeStr;

//整点距离
@property(nonatomic,assign)int unitDistance;

//记录已经播报过得总距离(下一次播报)
@property(nonatomic,assign)int totoalUintDistance;

@end


@implementation HMSportSpeaker

- (instancetype)initWithSportType:(HMSportType)type
{
    self = [super init];
    self.sportType = type;
    
    //设置默认单位距离,默认1km播报一次
    self.unitDistance = 1;
    //设置下一次播报距离
    self.totoalUintDistance = self.unitDistance;
    return self;
}

//运动开始播报
- (void)startSport
{
    NSString *str;
    switch (self.sportType) {
        case HMSportTypeRun:
            str = @"跑步";
            break;
        case HMSportTypeBike:
            str = @"骑行";
            break;
        case HMSportTypeWalk:
            str = @"走路";
            break;
            
        default:
            break;
    }
    
    self.sportTypeStr = str;
    
    //开始语音播报文本
    [self playVoiceWithText:[NSString stringWithFormat:@"开始%@",str]];
}

//运动状态改变播报
- (void)sportTypeChanged:(HMSportState)sportState
{
    NSString *str;
    switch (sportState) {
        case HMSportStatePause:
            str = @"运动已暂停";
            break;
        case HMSportStateFinish:
            str = @"休息一下吧";
            break;
        case HMSportStateContinue:
            str = @"运动已恢复";
            break;
            
        default:
            break;
    }
    
    //开始语音播报文本
    [self playVoiceWithText:str];
}

//整点距离的播报

/**
 播报整点距离
 */
- (void)reportDistance:(double)distance Time:(double)time Speed:(double)speed
{
    
    //1.如果当前距离大于播报总距离则开始播报
    if (distance > self.totoalUintDistance) {
        NSString *str = [NSString stringWithFormat:@"您已:%@ %.2f公里 用时%@ 平均速度:%.2f km/h",self.sportTypeStr,distance,[self getTimeStrWithTime:time],speed];
        //开始语音播报文本
        [self playVoiceWithText:str];
        //2.播报完成之后,下一次播报的整点距离应该加一个系数
        self.totoalUintDistance += self.unitDistance;
    }
}


//将浮点型时间转化为语音播报的时间字符串
- (NSString *)getTimeStrWithTime:(double)time
{
    NSMutableString *str = [[NSMutableString alloc] initWithString:@""];
    NSString *hour;
    NSString *minute;
    NSString *second;
    
    int h = time/3600;
    int m = (int)time%3600/60;
    int s = (int)time%60;
    //1.运动时间超过一小时才播报时间,没有超过一小时则不播报时间
    if (time/3600 >= 1) {
        
        hour = [NSString stringWithFormat:@"%d小时",h];
        [str appendString:hour];
    }
    
    //2.分钟应该大于0才播报,或者分钟为0,小时大于0
    if (h>0 || m>0) {
        minute = [NSString stringWithFormat:@"%d分",m];
        [str appendString:minute];
    }
    
    //3.秒正常情况下一定会有
    second = [NSString stringWithFormat:@"%d秒",s];
    [str appendString:second];
    
    return str;
    
}

//将文本变成声音
- (void)playVoiceWithText:(NSString *)text
{
    NSLog(@"%@",text);
    
    //1.创建语音合成器
    if (self.synthesizer == nil)
    {
        self.synthesizer = [[AVSpeechSynthesizer alloc] init];
    }
    
    //2.判断当前是否正在播报,如果在语音播报时,上一条语音还未播放完就导致线程阻塞,丢失部分声音
    /**我们使用结束声音的时候,语音合成器应该是全部变量
     AVSpeechBoundaryImmediate,立即结束还没有播放完的声音,直接播放本次的声音
     AVSpeechBoundaryWord,等上一条播放完再播放本次声音
     */
    if (self.synthesizer.isSpeaking)
    {
        [self.synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    }
    //3.使用文本创建要播放的语音(默认情况下播放系统的语音)
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:text];
    
    //4.指定播放的语音
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-Hans"];
    utterance.voice = voice;
    
    //声音其他属性介绍
    utterance.volume = 1.0; //设置音量0.0~1.0   默认为1.0
    utterance.rate = 0.5;  //设置语速
    utterance.pitchMultiplier = 1.0; //设置语调0.5~2
    
    //5.开始播报语音   参数:语音对象
    [self.synthesizer speakUtterance:utterance];

    
}

@end
